package com.yilin.tms.core.commons.zysdk;


import java.util.*;

public class SignCommonUtil {

    public static String signWithParamsAndUrlPath(String urlPath, Map<String, String> params, String secretKey) {
        List<String> paramValueList = new ArrayList();
        if (params != null) {
            Iterator var5 = params.entrySet().iterator();

            while(var5.hasNext()) {
                Map.Entry<String, String> entry = (Map.Entry)var5.next();
                paramValueList.add((String)entry.getKey() + (String)entry.getValue());
            }
        }

        String[] datas = new String[1 + paramValueList.size()];
        datas[0] = urlPath;
        Collections.sort(paramValueList);

        for(int i = 0; i < paramValueList.size(); ++i) {
            datas[i + 1] = (String)paramValueList.get(i);
        }

        byte[] signature = SecurityUtil.hmacSha1(datas, SignStringUtil.toBytes(secretKey));
        return SignStringUtil.encodeHexStr(signature);
    }

    public static String signWithParamsOnly(Map<String, String> params, String secretKey) {
        List<String> paramValueList = new ArrayList();
        if (params != null) {
            Iterator var4 = params.entrySet().iterator();

            while(var4.hasNext()) {
                Map.Entry<String, String> entry = (Map.Entry)var4.next();
                paramValueList.add((String)entry.getKey() + (String)entry.getValue());
            }
        }

        Collections.sort(paramValueList);
        String[] datas = new String[paramValueList.size()];
        paramValueList.toArray(datas);
        byte[] signature = SecurityUtil.hmacSha1(datas, SignStringUtil.toBytes(secretKey));
        return SignStringUtil.encodeHexStr(signature);
    }

    public static String getWholeUrl(String url, Map<String, String> params) {
        if (url == null) {
            return null;
        } else if (params == null) {
            return url;
        } else {
            Set<Map.Entry<String, String>> set = params.entrySet();
            if (set.size() <= 0) {
                return url;
            } else {
                url = url + "?";
                Iterator<Map.Entry<String, String>> it = set.iterator();
                Map.Entry entry;
                String param;
                if (it.hasNext()) {
                    entry = (Map.Entry)it.next();
                    param = (String)entry.getKey() + "=" + (String)entry.getValue();
                    url = url + param;
                }

                while(it.hasNext()) {
                    entry = (Map.Entry)it.next();
                    param = (String)entry.getKey() + "=" + (String)entry.getValue();
                    url = url + "&" + param;
                }

                return url;
            }
        }
    }

    private SignCommonUtil() {
    }
}
